import tim from '../commen/tim/tim'

export default {
	state: {
		isLogin: false,
		isSDKReady: false ,// TIM SDK 是否 ready
		
		conversationActive:{},	//聊天进行中的会话
		toUserId:'',			//聊天对象id
		conversationList:[],		//会话列表
		currentMessageList:[],			//消息列表
		currentDoctor:{},
		orderInfo:{},
		discountInfo:{},
		patientInfo:{},
		zjwzRole: 1,
		areaInfo: {},
		sectionInfo: {},
		hospitalInfo: {},
		hasUploadCase: false,
		caseInfo: {}
	},
	mutations: {
		setCaseInfo(state, caseInfo) {
			state.caseInfo = caseInfo
		},
		setHasUploadCase(state, bool) {
			state.hasUploadCase = bool
		},
		setPatientInfo: (state, patientInfo) => {
			state.patientInfo = patientInfo
		},
		setZjwzRole: (state, zjwzRole) => {
			state.zjwzRole = zjwzRole
		},
		setAreaInfo: (state, areaInfo) => {
			state.areaInfo = areaInfo
		},
		setSectionInfo: (state, sectionInfo) => {
			state.sectionInfo = sectionInfo
		},
		setHospitalInfo: (state, hospitalInfo) => {
			state.hospitalInfo = hospitalInfo
		},
		setOrderInfo: (state, orderInfo) => {
			state.orderInfo = orderInfo
		},
		setDiscountInfo: (state, discountInfo) => {
		  state.discountInfo = discountInfo
		},
		setCurrentDoctor: (state, currentDoctor) => {
			state.currentDoctor = currentDoctor
		},
		//更新登录状态
		toggleIsLogin(state, isLogin) {
			state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
		},
		//更新TIMSDK状态
		toggleIsSDKReady(state, isSDKReady) {
			state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
		},
		//退出登录重置状态
		reset(state) {
			state.isLogin = false
			state.isSDKReady = false
		},
		//选择好友聊天--创建会话/拼接会话id
		createConversationActive(state, [TYPES, toUserId]) {
			state.conversationActive.conversationID = TYPES + toUserId
			// state.conversationActive.conversationID = "C2C" + toUserId
			state.toUserId = toUserId
			state.currentMessageList = []
		},
		//选择已有会话聊天--更新选中会话详情
		updateConversationActive(state, conversationItem) {
			state.conversationActive = Object.assign({}, conversationItem.conversation);
			state.toUserId = conversationItem.user.userId
			state.currentMessageList = []
		},
		//更新会话列表
		updateConversationList(state, newConversationList) {
			state.conversationList = newConversationList
		},
		/**
		 * 将消息插入当前会话列表
		 * 调用时机：收/发消息事件触发时
		 * @param {Object} state
		 * @param {Message[]|Message} data
		 * @returns
		 */
		pushCurrentMessageList(state, data) {
			// 还没当前会话，则跳过
			if (Array.isArray(data)) {
				// 筛选出当前会话的消息
				const result = data.filter(item => item.conversationID === state.conversationActive.conversationID)
				state.currentMessageList = [...state.currentMessageList, ...result]
			} else if (data.conversationID === state.conversationActive.conversationID) {
				state.currentMessageList = [...state.currentMessageList, data]
			}
			// console.log( state.currentMessageList)
		},
		/**
		 * 滑到顶部请求更多的历史消息
		 * */
		unshiftCurrentMessageList(state, data) {
			if (data) {
				state.currentMessageList = [...data, ...state.currentMessageList]
			}
		},

	},
	getters: {
		getCurrentDoctor(state) {
			console.log(state.currentDoctor)
			return state.currentDoctor
		}
	},
	actions: {

	}
}
